package com.sphd.education.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.education.R;
import com.sphd.education.info.ClassifyOne;
import com.sphd.education.interfaces.RecyclerViewOnClickListener;

import java.util.List;



/**
 * Created by Administrator on 2015/10/16.
 * 左边对应课程类别的适配器
 */
public class ClassifyRecyclerAdapter extends RecyclerView.Adapter{
    private Context context;
    private List<ClassifyOne> classifyOneList;
    private int layoutInt;
    private int selectedPosition = -1;// 选中的位置
    private RecyclerViewOnClickListener recyclerViewOnClickListener;


    public ClassifyRecyclerAdapter(Context context, List<ClassifyOne> classifyOneList, int layoutInt) {
        this.context = context;
        this.classifyOneList = classifyOneList;
        this.layoutInt = layoutInt;
    }
    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewClassify viewClassify = new ViewClassify(LayoutInflater.from(parent.getContext()).inflate(
                layoutInt, parent, false
        ));
        return viewClassify;

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        ((ViewClassify) holder).position=position;
        ((ViewClassify) holder).fenlei.setText(classifyOneList.get(position).getClassify());
        if (selectedPosition == position) {
            ((ViewClassify) holder).linearLayoutList.setBackgroundColor(context.getResources().getColor(R.color.my_gr));
        } else {
            ((ViewClassify) holder).linearLayoutList.setBackgroundColor(Color.TRANSPARENT);
        }
    }


    @Override
    public int getItemCount() {
        return classifyOneList.size();
    }


    public void setRecyclerViewOnClickListener(RecyclerViewOnClickListener recyclerViewOnClickListener) {
        this.recyclerViewOnClickListener = recyclerViewOnClickListener;
    }



    /**
     * 左边控件的viewhold
     */
    class ViewClassify extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView fenlei;//分类的名字
        int position;// 用来区分是哪一个坐标
        LinearLayout linearLayoutList;//用于监听的父控件

        public ViewClassify(View itemView) {
            super(itemView);
            fenlei = (TextView) itemView.findViewById(R.id.classify_one_textview);
            linearLayoutList = (LinearLayout) itemView.findViewById(R.id.LinearLayoutList);
            linearLayoutList.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            recyclerViewOnClickListener.onTouch(v,position);
        }
    }

}
